package com.example.b_308.software.view.main;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.b_308.software.R;
import com.example.b_308.software.view.BaseFragment;
import com.example.b_308.software.view.WorkPageAdapter;
import com.example.b_308.software.view.WorkViewPage;
import com.example.b_308.software.view.base.IBaseView;
import com.example.b_308.software.view.main.contacts.ContactMessage;
import com.example.b_308.software.view.main.message.MessageFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IBaseView{
     WorkViewPage w_vp;
     RadioGroup rg;
    List<BaseFragment> fragments=new ArrayList<>();
    int current=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        w_vp=(WorkViewPage) findViewById(R.id.w_vp);
        rg=(RadioGroup)findViewById(R.id.rg);
        fragments.add(new MessageFragment());
        fragments.add(new ContactMessage());
        fragments.add(new WorkFragment());
        fragments.add(new MineFragment());
        w_vp.setAdapter((new WorkPageAdapter(getSupportFragmentManager(),fragments) {
            public  Fragment getItem(int position){
                return  fragments.get(position);
            }
            public int getCount(){
                return fragments.size();
            }
        }));

        w_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (current != position) {
                    ((RadioButton) rg.getChildAt(current)).setChecked(false);
                    ((RadioButton) rg.getChildAt(position)).setChecked(true);
                    current = position;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
       w_vp.setCurrentItem(0);
    }

    public void check(View view) {
        int id = rg.indexOfChild(view);
        w_vp.setCurrentItem(id);
    }

    @Override
    public void fail(String message) {

    }
}
